//
//  ELNewFeatureController.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/17.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit
import SDWebImage

class ELNewFeatureController: UIViewController {

    private var iconConstraint : NSLayoutConstraint?
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupSubView()
        //加载上一次头像
        setIconView()
        
        //加载用户信息
        ELUserAccount.loadUserAccount()?.loadingUserInfo({ (error) -> () in
            if error != nil {
                print(error)
                return
            }
            
            self.setIconView()
        })
    }
    
    //MARK: - 私有方法
    ///更新头像信息
    private func setIconView () {
        //如果有头像
        if let avatar_large = ELUserAccount.loadUserAccount()?.avatar_large {
            let url = NSURL(string: avatar_large)
            print("头头像url = \(url)")
            
            self.iconView.sd_setImageWithURL(url, placeholderImage:UIImage(named: "avatar_default_big"))
        }
    }
    
    ///设置子控件和约束
    private func setupSubView () {
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
//        bgImageView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //添加背景约束
//        view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        bgImageView.ff_Fill(self.view)
        
//        //添加头像约束
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        //距离底部160
//        iconConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconConstraint!)
//        ///约束宽高85
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        let iconViewCons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: self.view, size: CGSizeMake(85, 85), offset: CGPointMake(0, -160))
        iconConstraint = iconView.ff_Constraint(iconViewCons, attribute: NSLayoutAttribute.Bottom)
        
        //添加文字约束
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 16))
    }
    
    ///为头像设置动画
    private func moveAnimation () {
        //设置约束值
        let iconHeight : CGFloat = UIScreen.mainScreen().bounds.height - 160
        iconConstraint?.constant = -iconHeight
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //动画更新约束
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    //动画完成显示欢迎归来
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("头像动画显示完成")
                        UIApplication.sharedApplication().keyWindow?.rootViewController = ELMainTabBarController()
                })
        }
    }

    //MARK: - 懒加载
    ///背景
    private lazy var bgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///头像
    private lazy var iconView : UIImageView = {
        
        let image = UIImage(named: "avatar_default_big")
        let icon : UIImageView = UIImageView(image:image)
        
        icon.layer.cornerRadius = image!.size.width * 0.5
        icon.layer.masksToBounds = true
        
        return icon
    }()
    ///欢迎归来文字
    private lazy var welcomeLabel : UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.textColor = UIColor.darkGrayColor()
        label.alpha = 0
        return label
    }()
}
